<?php

namespace App\Http\Controllers\Callback\Pay;

use App\Enums\Check\CheckEnum;
use App\Http\Controllers\BaseController;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Service\OpenShop\Wap\SignOrderService;
use App\Service\Pay\CallbackService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PayController extends BaseController
{
    public function pay(Request $request)
    {
        // 统一处理
        (new CallbackService())->entrance($request);

        return $this->success();
    }

    /**
     * 开店相关支付成功回调
     * @param Request $request
     * @return JsonResponse
     */
    public function openShopPaySuccess(Request $request): JsonResponse
    {
        $shopId     = $request->get('shopId');
        $bizId      = $request->get('bizId');
        $payPrice   = $request->get('payPrice');
        $payChannel = $request->get('payChannel');
        $payNo      = $request->get('payNo');
        $sysType    = $request->get('sysType');

        $orderNo = OpenShopMainProcessModel::query()->where('shopId', $shopId)->orderByDesc('id')->value('orderNo');
        if (!$orderNo) {
            return $this->fail(400, '不正确的shopId');
        }

        (new SignOrderService($orderNo, null, false))->internalPaySuccess($bizId, $payPrice, $payChannel, $payNo, $sysType);

        return $this->success();
    }
}
